package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.live.activity.R;
import com.live.common.LiveImageLoader;
import com.live.common.LiveImageLoader.ImageCallback;

public class LiveVideoSA extends SimpleAdapter {

	public List<Map<String, Object>> reviewList = null;
	public Context context = null;

	private LiveImageLoader imageLoader;
	private ListView listView;

	public LiveVideoSA(Context context, List<Map<String, Object>> items, int resource, String[] from,
			int[] to, ListView listView) {
		super(context, items, resource, from, to);
		this.context = context;
		this.listView = listView;
		imageLoader = new LiveImageLoader();
	}

	@Override
	@SuppressWarnings("unchecked")
	public View getView(final int position, View convertView, ViewGroup parent) {
		Map<String, Object> review = (Map<String, Object>) getItem(position);

		convertView = super.getView(position, convertView, parent);

		ImageView videoImg = (ImageView) convertView.findViewById(R.id.videoImg);

		String imageUrl = (String) review.get("videoImgUrl");
		videoImg.setTag(imageUrl);
		Drawable cachedImage = imageLoader.loadDrawable(imageUrl, new ImageCallback() {
			public void imageLoaded(Drawable imageDrawable, String imageUrl) {
				ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
				if (imageViewByTag != null) {
					imageViewByTag.setImageDrawable(imageDrawable);
				}
			}
		});
		if (cachedImage == null) {
			videoImg.setImageResource(R.drawable.default_v);
		} else {
			videoImg.setImageDrawable(cachedImage);
		}

		return convertView;
	}
}